﻿using HandyControl.Controls;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AgileLogistics.View.User.Dep
{
    /// <summary>
    /// Page_List.xaml 的交互逻辑
    /// </summary>
    public partial class Page_List : Page
    {
        ViewModel.BaseViewModel vm;

        Model.DBContext dbContext = new Model.DBContext();
        Model.Repository<Model.Depment> Rep = new Model.Repository<Model.Depment>();
        public Page_List()
        {
            InitializeComponent();
            vm = DataContext as ViewModel.BaseViewModel;
            vm.DepList =   Rep.GetList() ;

           
        }

        private void btn_New_Click(object sender, RoutedEventArgs e)
        {
            var win = new Add();
            win.Show();
        }

        private void btn_Del_Click(object sender, RoutedEventArgs e)
        {
            if(vm.CurrDep ==null)
            {
                Growl.WarningGlobal("请选择要删除的类型！");
                return;
            }

            Rep.Delete(vm.CurrDep);
            Growl.SuccessGlobal("删除成功！"); 
            vm.DepList = Rep.GetList() ;
        }

        private void SearchBar_SearchStarted(object sender, HandyControl.Data.FunctionEventArgs<string> e)
        {
            vm.DepList =   dbContext.DB.Queryable<Model.Depment>().Where(u=>u.Name.Contains( e.Info) || u.Id.ToString()  == e.Info ).ToList() ;
        }

        private void btn_Edit_Click(object sender, RoutedEventArgs e)
        {
            if(vm.CurrDep ==null)
            {
                Growl.WarningGlobal("请选择要编辑的部门");
                return;
            }
             
            var  win = new Edit();
            win.Show();
        }
    }
}
